package com.palrevo.beackBeans;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import com.palrevo.common.Constants;
import com.palrevo.data.dao.DataException;
import com.palrevo.data.models.AboutUs;
import com.palrevo.data.models.Advertisement;
import com.palrevo.data.models.Menu;
import com.palrevo.data.models.Page;
import com.palrevo.data.services.AdvertisementDBService;
import com.palrevo.data.services.CommonDBService;
import com.palrevo.data.services.PagesDBService;
import com.palrevo.utils.Configs;
import com.palrevo.utils.FileUtilites;
import com.palrevo.utils.WebUtils;

/**
 * 
 * @author AAsha
 *
 */
@ManagedBean(name="appBean")
@ApplicationScoped
public class ApplicationBean {
	
	private Menu menu = new Menu();
	
	private Map<Long, Advertisement> advers = new HashMap<Long, Advertisement>();
	private Map<Long, Advertisement> sideAdvers = new HashMap<Long,Advertisement>();
	private Map<Long, Advertisement> sponsorAdvers = new HashMap<Long,Advertisement>();
	
	private Map<Long, Page> pages = new HashMap<Long, Page>();
	
	private AboutUs aboutUs = new AboutUs();
	
	public ApplicationBean() {
		super();
	}
	
	public void loadSiteContents() throws Exception, IOException{
		this.copyImages();
		this.loadMainAdvers();
		this.loadSideAdvers();
		this.loadSponsorAdvers();
		this.loadMenu();
		this.loadPages();
		this.loadAboutUsPage();
	}
	
	private void loadMainAdvers() throws DataException, IOException{
		advers = AdvertisementDBService.getInstance().loadMainAdver();
	}
	
	private void copyImages() throws IOException {
		
		String fromDir = Configs.getConfigValue(Constants.Configs.M_ADVS_FILES_DIR);
		String toDir = WebUtils.getResourcesDir().concat(File.separator).concat("images").concat(File.separator).concat("m_advs");
		FileUtilites.copyImages(fromDir, toDir);
		
		fromDir = Configs.getConfigValue(Constants.Configs.S_ADVS_FILES_DIR);
		toDir = WebUtils.getResourcesDir().concat(File.separator).concat("images").concat(File.separator).concat("s_advs");
		FileUtilites.copyImages(fromDir, toDir);
		
		fromDir = Configs.getConfigValue(Constants.Configs.SP_ADVS_FILES_DIR);
		toDir = WebUtils.getResourcesDir().concat(File.separator).concat("images").concat(File.separator).concat("sp_advs");
		FileUtilites.copyImages(fromDir, toDir);
		
		fromDir = Configs.getConfigValue(Constants.Configs.ABOUT_US_FILES_DIR);
		toDir = WebUtils.getResourcesDir().concat(File.separator).concat("images").concat(File.separator).concat("about_us");
		FileUtilites.copyImages(fromDir, toDir);
	}
	
	private void loadSideAdvers() throws DataException{
		sideAdvers = AdvertisementDBService.getInstance().loadSideAdver();
	}
	
	private void loadSponsorAdvers() throws DataException{
		sponsorAdvers = AdvertisementDBService.getInstance().loadSponsorAdver();
	}
	
	private void loadMenu() throws DataException{
		menu = PagesDBService.getInstance().loadMenu();
	}
	
	private void loadPages() throws DataException{
		setPages(PagesDBService.getInstance().loadContentPages());
	}
	
	private void loadAboutUsPage() throws DataException{
		setAboutUs(CommonDBService.getInstance().loadAboutUs());
		
	}
	
	//================================================
	//				Setters and Getters
	//================================================
	
	

	/**
	 * @return the advers as list
	 */
	public List<Advertisement> getAdvers() {
		return new ArrayList<Advertisement>(advers.values());
	}
	
	/**
	 * @return the advers
	 */
	public Map<Long, Advertisement> getAdversMap() {
		return advers;
	}

	/**
	 * 
	 * @return
	 */
	public List<Advertisement> getSideAdvers() {
		return new ArrayList<Advertisement>(sideAdvers.values());
	}

	/**
	 * 
	 * @return
	 */
	public List<Advertisement> getSponsorAdvers() {
		return new ArrayList<Advertisement>(sponsorAdvers.values());
	}

	/**
	 * @return the menu
	 */
	public Menu getMenu() {
		return menu;
	}

	/**
	 * @param menu the menu to set
	 */
	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	/**
	 * @return the pages
	 */
	public Map<Long, Page> getPages() {
		return pages;
	}

	/**
	 * @param pages the pages to set
	 */
	public void setPages(Map<Long, Page> pages) {
		this.pages = pages;
	}

	/**
	 * @return the aboutUs
	 */
	public AboutUs getAboutUs() {
		return aboutUs;
	}

	/**
	 * @param aboutUs the aboutUs to set
	 */
	public void setAboutUs(AboutUs aboutUs) {
		this.aboutUs = aboutUs;
	}	
	
}
